package com.shopmall.manager.controller;

import com.github.pagehelper.PageInfo;
import com.shopmall.manager.service.ProductSpecService;
import com.shopmall.model.entity.product.ProductSpec;
import com.shopmall.model.vo.common.Result;
import com.shopmall.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product/productSpec")
public class ProductSpecController {
    @Autowired
    private ProductSpecService productSpecService;

    // 全部列表
    @GetMapping("/findAll")
    public Result<List<ProductSpec>> findAll() {
        List<ProductSpec> list = productSpecService.findAll();
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    // 列表
    @GetMapping("/{page}/{limit}")
    public Result<PageInfo<ProductSpec>> list(@PathVariable Integer page,
                                              @PathVariable Integer limit) {
        PageInfo<ProductSpec> pageInfo = productSpecService.findByPage(page, limit);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    // 添加
    @PostMapping("/save")
    public Result<String> save(@RequestBody ProductSpec productSpec) {
        String flag = productSpecService.save(productSpec);
        return Result.build(flag, ResultCodeEnum.SUCCESS);
    }

    // 修改
    @PutMapping("/updateById")
    public Result<String> updateById(@RequestBody ProductSpec productSpec) {
        String flag = productSpecService.updateById(productSpec);
        return Result.build(flag, ResultCodeEnum.SUCCESS);
    }

    // 删除
    @DeleteMapping("/deleteById/{id}")
    public Result<String> deleteById(@PathVariable Long id) {
        String flag = productSpecService.deleteById(id);
        return Result.build(flag, ResultCodeEnum.SUCCESS);
    }
}
